home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dev / java_dev.idb / usr / demos / java / Impression / Impression.java.z / Impression.java
Encoding:
Java Source  |  2000-05-20  |  8.7 KB  |  364 lines

  1. /*
  2.  *    impression -
  3.  *        This is a paint program that implements an Impressionist
  4.  *    painting technique based on image sampling.
  5.  *
  6.  *                Paul Haeberli - 1995
  7.  *                    paul@sgi.com
  8.  *
  9.  *             This painting technique is covered by 
  10.  *             U.S patent Number 5,182,548
  11.  *
  12.  */
  13. import java.awt.*;
  14. import java.lang.*;
  15. import java.net.*;
  16. import java.awt.image.*;
  17. import java.util.Random;
  18. import java.io.PrintStream;
  19.  
  20. class MemoryImage implements ImageConsumer {
  21.     boolean imgready;
  22.     int imgxsize, imgysize;
  23.     int imgpixels[][];
  24.  
  25.     MemoryImage(Image picture) {
  26.     int ticks;
  27.  
  28.     picture.getSource().startProduction(this);
  29.     ticks = 5*60*1000; /* 5 minutes */
  30.     while(ticks>0) {
  31.         try {
  32.         Thread.currentThread().sleep(100);
  33.         } catch (Exception e) { ; }
  34.         if(this.imgready)
  35.         break;
  36.         ticks -= 100;
  37.     }
  38.     }
  39.     public void setProperties(java.util.Hashtable dummy) {
  40.     }
  41.     public void setColorModel(ColorModel dummy) {
  42.     }
  43.     public void setHints(int dummy) {
  44.     }
  45.     public void imageComplete(int dummy) {
  46.     imgready = true;
  47.     }
  48.     public void setDimensions(int x, int y) {
  49.     imgxsize = x;
  50.     imgysize = y;
  51.     imgpixels = new int[y][x];
  52.     }
  53.     public void setPixels(int x1, int y1, int w, int h, 
  54.     ColorModel model, byte pixels[], int off, int scansize) {
  55.     int x, y, x2, y2, sx, sy;
  56.  
  57.     x2 = x1+w;
  58.     y2 = y1+h;
  59.     sy = off;
  60.     for(y=y1; y<y2; y++) {
  61.         sx = sy;
  62.         for(x=x1; x<x2; x++) 
  63.         imgpixels[y][x] = model.getRGB(pixels[sx++]);
  64.         sy += scansize;
  65.     }
  66.     }
  67.     public void setPixels(int x1, int y1, int w, int h, 
  68.     ColorModel model, int pixels[], int off, int scansize) {
  69.     int x, y, x2, y2, sx, sy;
  70.  
  71.     x2 = x1+w;
  72.     y2 = y1+h;
  73.     sy = off;
  74.     for(y=y1; y<y2; y++) {
  75.         sx = sy;
  76.         for(x=x1; x<x2; x++) 
  77.         imgpixels[y][x] = model.getRGB(pixels[sx++]);
  78.         sy += scansize;
  79.     }
  80.     }
  81. }
  82.  
  83. final public class Impression extends java.applet.Applet {
  84.     Random r;
  85.     Graphics canvasG, myG;
  86.     Image canvasimage, picture, menu;
  87.     int brushsize, paintmode;
  88.     int bsize, bsize2;
  89.     int jsize, jsize2;
  90.     int lx, ly, brushtype;
  91.     boolean lok;
  92.     MemoryImage memimg;
  93.     int wxsize, wysize;
  94.     int cxsize, cysize;
  95.     int dxsize, dysize;
  96.     boolean dopaint;
  97.     Font font;
  98.     FontMetrics fm;
  99.     Color border, canvas, text;
  100.     int nstrokes[] = { 5, 5, 5, 5, 10 };
  101.     int controlheight;
  102.     int borderwidth;
  103.     int brushsteps;
  104.  
  105.     public void init() {
  106.     controlheight = 30;
  107.     borderwidth = 1;
  108.     brushsteps = 8;
  109.     brushtype = 2;
  110.  
  111.     wxsize = size().width;
  112.     wysize = size().height;
  113.     cxsize = wxsize;
  114.     cysize = wysize-controlheight;
  115.     dxsize = cxsize-2*borderwidth;
  116.     dysize = cysize-2*borderwidth;
  117.  
  118.     resize(wxsize,wysize);
  119.  
  120.     canvasimage = createImage(dxsize,dysize);
  121.      myG = getGraphics();
  122.     canvasG = canvasimage.getGraphics();
  123.  
  124.     font = new java.awt.Font("TimesRoman", Font.ITALIC, 14);
  125.         canvasG.setFont(font);
  126.         fm = canvasG.getFontMetrics();
  127.         lok = false;
  128.     r = new Random();
  129.     setbrushsize(5);
  130.     paintmode = 1;
  131.     }
  132.     private int addjitter(int val) {
  133.     return val + (Math.abs(r.nextInt())%jsize) - jsize2;
  134.     }
  135.     private Color samplecolor(int x, int y) {
  136.     x = (x*memimg.imgxsize)/dxsize;
  137.     y = (y*memimg.imgysize)/dysize;
  138.     if(x<0) x = 0;
  139.     if(x>=memimg.imgxsize) x=memimg.imgxsize-1;
  140.     if(y<0) y = 0;
  141.     if(y>=memimg.imgysize) y=memimg.imgysize-1;
  142.     return new Color(memimg.imgpixels[y][x]);
  143.     }
  144.     private void drawbrush(Graphics g, int lx, int ly, int cx, int cy, int nsteps, boolean demo) {
  145.         int i, x, y, rx, ry, px, py, del;
  146.         int m, nmarks;
  147.  
  148.         for(i=0; i<nsteps; i++) {
  149.             px = (lx*(nsteps-i)+i*cx)/nsteps;
  150.             py = (ly*(nsteps-i)+i*cy)/nsteps;
  151.         if(demo) {
  152.         x = px;
  153.         y = py;
  154.         } else {
  155.         x = addjitter(px);
  156.         y = addjitter(py);
  157.         g.setColor(samplecolor(x,y));
  158.         }
  159.             switch(brushtype) {
  160.                 case 0:
  161.                     g.fillOval(x-bsize2,y-bsize2,bsize,bsize);
  162.                     break;
  163.                 case 1:
  164.             if(demo)
  165.             nmarks = 7;
  166.             else
  167.             nmarks = 1+(bsize*bsize)/200;
  168.                     for(m=0; m<nmarks; m++) {
  169.                         rx = addjitter(x);
  170.                         ry = addjitter(y);
  171.                         g.drawLine(rx-1,ry+1,rx+1,ry-1);
  172.                     }
  173.                     break;
  174.                 case 2:
  175.                     g.drawLine(x-bsize2,y+bsize2,x+bsize2,y-bsize2);
  176.                     break;
  177.                 case 3:
  178.             if(demo)
  179.             nmarks = 8;
  180.             else
  181.             nmarks = 2+(bsize)/3;
  182.             px = x-bsize2;        
  183.             py = y+bsize2;        
  184.                     for(m=0; m<nmarks; m++) {
  185.             del = (Math.abs(r.nextInt())%bsize);
  186.                 x = px + del;
  187.                 y = py - del;
  188.                         g.drawLine(x,y,x,y);
  189.                     }
  190.                     break;
  191.                 case 4:
  192.             g.drawLine(x,y,x,y);
  193.                     break;
  194.         }
  195.         }
  196.     }
  197.     private void showbrushsize() {
  198.     int xorg, yorg, mpos, barwidth, barheight, markwidth;
  199.  
  200.      xorg = 146;
  201.      yorg = cysize+10;
  202.      barwidth = 37;
  203.      barheight = 5;
  204.      markwidth = 3;
  205.        mpos = (int)((barwidth-markwidth)*(brushsize/(float)brushsteps));
  206.     myG.setColor(Color.white);
  207.     myG.fillRect(xorg,yorg,barwidth,barheight);
  208.     myG.setColor(Color.red);
  209.     myG.fillRect(xorg+mpos,yorg,markwidth,barheight);
  210.     }
  211.     private void showbrushtype() {
  212.     int xorg, yorg, oldsize;
  213.  
  214.     xorg = 277;
  215.     yorg = cysize+18;
  216.     oldsize = brushsize;
  217.     setbrushsize(3);
  218.     myG.setColor(Color.white);
  219.     myG.fillRect(xorg-15,yorg-15,30,30);
  220.     myG.setColor(Color.black);
  221.     drawbrush(myG,xorg,yorg,xorg,yorg,1,true);
  222.     setbrushsize(oldsize);
  223.     }
  224.     private void nextbrush() {
  225.     brushtype = (brushtype+1)%5;
  226.     showbrushtype();
  227.     }
  228.     private void setbrushsize(int newbrushsize) {
  229.     float diam;
  230.  
  231.     brushsize = newbrushsize;
  232.     diam = (int)(4.0*Math.pow(1.41,newbrushsize));
  233.     bsize2 = (int)(diam/2.0);
  234.     bsize = 2*bsize2+1;
  235.     jsize2 = (int)(2.0*diam/2.0);
  236.     jsize = 2*jsize2+1;
  237.     }
  238.     private void biggerbrush() {
  239.     if(brushsize<brushsteps) {
  240.         setbrushsize(brushsize+1);
  241.         showbrushsize();
  242.     }
  243.     }
  244.     private void smallerbrush() {
  245.     if(brushsize>0) {
  246.         setbrushsize(brushsize-1);
  247.         showbrushsize();
  248.     }
  249.     }
  250.     private void showtext(String word) {
  251.     canvasG.setColor(text);
  252.     canvasG.drawString(word,(wxsize-fm.stringWidth(word))/2, wysize/3);
  253.     }
  254.     private void clearscreen() {
  255.     canvasG.setColor(canvas);
  256.     canvasG.fillRect(0,0,dxsize,dysize);
  257.     switch(paintmode) {
  258.         case 1:
  259.         showtext("Loading image . . . ");
  260.         break;
  261.         case 2:
  262.         showtext("Paint Here to Reveal. . .");
  263.         break;
  264.     }
  265.     }
  266.     public void update(Graphics g) {
  267.     paint(g);
  268.     }
  269.     public boolean mouseDown(Event evt, int x, int y) {
  270.     if(x>0 && x<cxsize && y>0 && y<cysize) {
  271.         if(paintmode == 2) {
  272.         paintmode++;
  273.         clearscreen();
  274.         updatescreen(myG);
  275.         }
  276.         dopaint = true;
  277.         lok = false;
  278.     } else if(x>0 && x<cxsize && y>cysize+1 && y<wysize) {
  279.         if(x>0 && x<57) {
  280.         clearscreen();
  281.         updatescreen(myG);
  282.         } else if(x>80 && x<161)
  283.         smallerbrush();
  284.         else if(x>181 && x<259)
  285.         biggerbrush();
  286.         else if(x>261 && x<291)
  287.         nextbrush();
  288.     }
  289.     return true;
  290.     }
  291.     public boolean mouseUp(Event evt, int x, int y) {
  292.     dopaint = false;
  293.     return true;
  294.     }
  295.     public boolean mouseDrag(Event evt, int x, int y) {
  296.     if(dopaint) {
  297.         if(lok) {
  298.         if((lx == x) && (ly == y))
  299.             return true;
  300.         drawbrush(canvasG,lx,ly,x,y,nstrokes[brushtype],false);
  301.         updatescreen(myG);
  302.         } else {
  303.         lok = true;
  304.         }
  305.         lx = x;
  306.         ly = y;
  307.     }
  308.     return true;
  309.     }
  310.     public void updatescreen(Graphics g) {
  311.     g.drawImage(canvasimage,1,1,this);
  312.     }
  313.     private void updateall(Graphics g) {
  314.     g.setColor(border);
  315.     g.drawRect(0,0,cxsize-1,cysize-1);
  316.     g.setColor(Color.white);
  317.     g.fillRect(0,cysize,wxsize,controlheight);
  318.     updatescreen(g);
  319.     g.drawImage(menu,0,cysize+4,this);
  320.     }
  321.     private Color paramcolor(String name, Color defcolor) {
  322.     String pval;
  323.      Integer i;
  324.  
  325.     if((pval=getParameter(name)) != null) {
  326.         i = Integer.valueOf(pval,16);
  327.         return new Color(i.intValue());
  328.     } else {
  329.         return defcolor;
  330.     }
  331.     }
  332.     public void paint(Graphics g) {    
  333.     String sourcename;
  334.  
  335.     if(paintmode == 1) {
  336.         border = paramcolor("bordercolor", new Color(190,190,190));
  337.         canvas = paramcolor("canvascolor", new Color(231,215,199));
  338.         text = paramcolor("textcolor", new Color(20,20,20));
  339.         sourcename = getParameter("source");
  340.         clearscreen();
  341.         menu = getImage(getCodeBase(),"menu.gif");
  342.         updateall(g);
  343.         if(sourcename.startsWith("http://")) {
  344.         try {
  345.             picture = getImage(new URL(sourcename));
  346.         } catch (Exception e) { 
  347.             System.out.println("impression: bad URL spec");
  348.         }
  349.         } else {
  350.         picture = getImage(getDocumentBase(),sourcename);
  351.         }
  352.         memimg = new MemoryImage(picture);
  353.         if(memimg.imgpixels == null)
  354.         System.out.println("impression: error on image read!");
  355.         paintmode = 2;
  356.         clearscreen();
  357.         updatescreen(g);
  358.     } else 
  359.         updateall(g);
  360.     showbrushsize();
  361.     showbrushtype();
  362.     }
  363. }
  364.